import Cookies from 'js-cookie'
const indexUrl = window.location.protocol + '\/\/' + window.location.host + '/ips/index.html#/dashboardPage'
// const indexUrl = window.location.protocol + '\/\/' + window.location.host + '\/' + 'index.html#' + '\/' + 'dashboardPage'
// const indexUrl = 'http://localhost:9529/#/'
// const indexUrl = 'http://172.16.42.196:9528/#/'
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    // opened: true,
    withoutAnimation: false
  },
  device: 'desktop',
  iframeSrc: indexUrl,
  tagsViewList: [{ children: null,
    component: 'core/iframe',
    firstMenuId: 'admin',
    folder: false,
    icon: null,
    id: 'index',
    resuId: '0',
    title: '首页',
    url: indexUrl }],
  tagsAciveIndex: 'index'
}
const mutations = {
  // 切换tab标签active
  CHANGE_TAB_ACTIVE: (state, active) => {
    state.tagsAciveIndex = active
  },
  CLEAR_TAB_ACTIVE: (state) => {
    state.tagsViewList = [{ children: null,
      component: 'core/iframe',
      firstMenuId: 'admin',
      folder: false,
      icon: null,
      id: 'index',
      resuId: '0',
      title: '首页',
      url: indexUrl }]
  },
  // 删除TAB栏菜单项
  REMOVE_TAB_MENU: (state, data) => {
    state.tagsViewList.splice(state.tagsViewList.findIndex(item => item.id === data), 1)
  },
  // 点击切换菜单
  CHANGE_MENU_DATA: (state, obj) => {
    let hasMenu = false
    state.tagsViewList.forEach(item => {
      if (item.id === obj.id) {
        hasMenu = true
        return
      }
    })
    if (!hasMenu) {
      state.tagsViewList.push(obj)
    }
  },
  // 切换路由
  CHANGE_IFRAME_SRC: (state, src) => {
    state.iframeSrc = src
  },
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
